package it.eng.area118.mdocommon.android;

import it.eng.area118.mdocommon.android.log.BasicLogger;
import it.eng.area118.mdocommon.android.util.AndroidApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.util.Log;

public class SharedPreferencesManager implements
		OnSharedPreferenceChangeListener {

	private final static String TAG = "SharedPreferencesManager";

	private static SharedPreferencesManager INSTANCE;

	private SharedPreferences sharedPreferencesInstance;

	private List<SharedPreferenceCallback> listeners = new ArrayList<SharedPreferenceCallback>();

	private static Object object = new Object();

	private SharedPreferencesManager() {
		// AndroidApplicationContext.getInstance().putApplicationContextEntry(
		// ContextMapKeyEnum.CONFIGURATIONS.name(),
		sharedPreferencesInstance = AndroidApplicationContext.getInstance()
				.getSharedPreferences();
		Map<String, ?> map = AndroidApplicationContext.getInstance()
				.getSharedPreferences().getAll();
		BasicLogger.writeLog(TAG, "Last configurations loaded: ");
		if (map != null) {
			for (String key : map.keySet()) {
				Object value = map.get(key);
				BasicLogger.writeLog(TAG, "Key: " + key + " - Value: " + value);

			}
		}

	}

	public synchronized static SharedPreferencesManager createInstance() {
		synchronized (object) {
			if (INSTANCE == null) {
				INSTANCE = new SharedPreferencesManager();
			}
			return INSTANCE;
		}

	}

	public synchronized static SharedPreferencesManager getInstance() {
		synchronized (object) {
			if (INSTANCE == null) {
				throw new NullPointerException(
						"SharedPreferencesManager instance never create... use createInstance first!");
			}
			return INSTANCE;
		}

	}

	public void init() {
		sharedPreferencesInstance
				.registerOnSharedPreferenceChangeListener(this);
	}

	public void reinit(SharedPreferences sharedPreferences) {
		if (sharedPreferencesInstance == null) {
			sharedPreferencesInstance = sharedPreferences;
			sharedPreferencesInstance
					.registerOnSharedPreferenceChangeListener(this);
			BasicLogger.writeLog(TAG, "Initialized...!");
		} else {
			Log.w(TAG, "Can't re-initialize on same instance: destroy first!");

		}

	}

	public void destroy() {
		sharedPreferencesInstance = null;
		// AndroidApplicationContext.getInstance().putApplicationContextEntry(ContextMapKeyEnum.CONFIGURATIONS.name(),
		// sharedPreferencesInstance);
		listeners = null;
		sharedPreferencesInstance
				.unregisterOnSharedPreferenceChangeListener(this);

	}

	public void addSharedPreferencesListener(SharedPreferenceCallback callback) {
		listeners.add(callback);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {

		for (SharedPreferenceCallback callback : listeners) {
			callback.onPreferenceChanged(key,
					sharedPreferences.getAll().get(key));
		}

	}

	// public SharedPreferences getSharedPreferencesInstance() {
	// return sharedPreferencesInstance;
	// }
	//
	// public void setSharedPreferencesInstance(
	// SharedPreferences sharedPreferencesInstance) {
	// this.sharedPreferencesInstance = sharedPreferencesInstance;
	// }

}
